﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;
using System.Net;
using System.IO;
using System.Web.Script.Serialization;
using AplicacionMVC.Models;


namespace AplicacionMVC.Controllers
{
    public class SalaRecursoController : Controller
    {
        private string salaRecursoRESTService = "http://localhost:1178/SalaRecursos.svc/SalasRecursos";
        JavaScriptSerializer js = new JavaScriptSerializer();

        // GET: /SalaRecurso/
        public ActionResult RecursosSala()
        {
            HttpWebRequest req2 = (HttpWebRequest)WebRequest.Create(salaRecursoRESTService + "/ListarSalas");
            req2.Method = "GET";
            HttpWebResponse res2 = (HttpWebResponse)req2.GetResponse();
            StreamReader reader2 = new StreamReader(res2.GetResponseStream());
            string salaJson = reader2.ReadToEnd();
            List<SalaRecurso> salaJson2 = js.Deserialize<List<SalaRecurso>>(salaJson);

            return View(salaJson2);
        }

        // GET: /SalaRecurso/Details/5
        public ActionResult DetailsU(string id)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(salaRecursoRESTService + "/ListarSalasRecursos/" + id);
            req.Method = "GET";
            HttpWebResponse res2 = (HttpWebResponse)req.GetResponse();
            StreamReader reader2 = new StreamReader(res2.GetResponseStream());
            string salaJson = reader2.ReadToEnd();
            List<SalaRecurso> salaJson2 = js.Deserialize<List<SalaRecurso>>(salaJson);
            return View(salaJson2);
        }

        // GET: /SalaRecurso/
        public ActionResult Index()
        {
            HttpWebRequest req2 = (HttpWebRequest)WebRequest.Create(salaRecursoRESTService + "/ListarSalas");
            req2.Method = "GET";
            HttpWebResponse res2 = (HttpWebResponse)req2.GetResponse();
            StreamReader reader2 = new StreamReader(res2.GetResponseStream());
            string salaJson = reader2.ReadToEnd();
            List<SalaRecurso> salaJson2 = js.Deserialize<List<SalaRecurso>>(salaJson);

            return View(salaJson2);
        }

        // GET: /SalaRecurso/Details/5
        public ActionResult Details(string id)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(salaRecursoRESTService + "/ListarSalasRecursos/" + id);
            req.Method = "GET";
            HttpWebResponse res2 = (HttpWebResponse)req.GetResponse();
            StreamReader reader2 = new StreamReader(res2.GetResponseStream());
            string salaJson = reader2.ReadToEnd();
            List<SalaRecurso> salaJson2 = js.Deserialize<List<SalaRecurso>>(salaJson);
            return View(salaJson2);
        }

        // GET: /SalaRecurso/Create
        public ActionResult Create(string id)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(salaRecursoRESTService + "/CrearSalaRecurso/" + id);
            req.Method = "GET";
            HttpWebResponse res2 = (HttpWebResponse)req.GetResponse();
            StreamReader reader2 = new StreamReader(res2.GetResponseStream());
            string salaJson = reader2.ReadToEnd();
            List<SalaRecurso> salaJson2 = js.Deserialize<List<SalaRecurso>>(salaJson);
            //return View(salaJson2);
            return RedirectToAction("Index");
        }


        // POST: //Sala/Edit/5
        [HttpPost]
        public ActionResult Create(string id, FormCollection collection)
        {
            try
            {
                string postdata = "{\"id_salarec\":\"" + collection["id_salarec"] + "\",\"nombre\":\"" + collection["nombre"] + "\",\"tiposala\":\"" + collection["tiposala"] + "\",\"horario\":\"" + collection["horario"] + "\",\"estado\":\"" + collection["estado"] + "\"}";
                byte[] data = Encoding.UTF8.GetBytes(postdata);
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(salaRecursoRESTService);
                req.Method = "PUT";
                req.ContentLength = data.Length;
                req.ContentType = "application/json";
                var reqStream = req.GetRequestStream();
                reqStream.Write(data, 0, data.Length);
                var res = (HttpWebResponse)req.GetResponse();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: /Sala/Edit/5 
        public ActionResult Edit(string id)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(salaRecursoRESTService + "/ListarRecursos/" + id);
            req.Method = "GET";
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(res.GetResponseStream());
            string salaJson = reader.ReadToEnd();
            List<SalaRecurso> salaJson2 = js.Deserialize<List<SalaRecurso>>(salaJson);
            //SalaRecurso sala = js.Deserialize<SalaRecurso>(salaJson);
            Session["data"] = "";
            return View(salaJson2);
        }

        // POST: //Sala/Edit/5
        [HttpPost]
        public ActionResult Edit(string id, FormCollection collection)
        {
            try
            {
                string postdata = "{\"id_salarec\":\"" + collection["id_salarec"] + "\",\"nombre\":\"" + collection["nombre"] + "\",\"tiposala\":\"" + collection["tiposala"] + "\",\"horario\":\"" + collection["horario"] + "\",\"estado\":\"" + collection["estado"] + "\"}";
                byte[] data = Encoding.UTF8.GetBytes(postdata);
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(salaRecursoRESTService);
                req.Method = "PUT";
                req.ContentLength = data.Length;
                req.ContentType = "application/json";
                var reqStream = req.GetRequestStream();
                reqStream.Write(data, 0, data.Length);
                var res = (HttpWebResponse)req.GetResponse();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: /SalaRecurso/Delete/5
        public ActionResult Delete(string id)
        {
            try
            {
                string postdata = "\"" + id + "\"";
                byte[] data = Encoding.UTF8.GetBytes(postdata);
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(salaRecursoRESTService);
                req.Method = "DELETE";
                req.ContentLength = data.Length;
                req.ContentType = "application/json";
                var reqStream = req.GetRequestStream();
                reqStream.Write(data, 0, data.Length);
                var res = (HttpWebResponse)req.GetResponse();
                Session["data"] = "";
                return RedirectToAction("Index");
            }
            catch (Exception e)
            {
                ModelState.AddModelError("Error", e.Message);
                return View();
            }
        }

    }
}
